package com.mkvld.appietijdenv2.data;

import android.content.Context;

import com.mkvld.appietijdenv2.activities.ActMainV9;
import com.mkvld.appietijdenv2.databases.HolidayDatabase;
import com.mkvld.appietijdenv2.databases.ShiftDatabase;

public class Payment {
	public static final double VACATION_PERC = 0.1109;
	public static final double ADV_PERC = 0.0901;
	public static final double VACATION_ALLOWANCE_PERC = 0.0961;
	public static final double WUK_PERC = 0.0327;
	public static final double KPT_GROSS_PERC = 1.3298;
	
	public double[] data;
	public final static int WORKED = 0;
	public final static int ALLOWANCE = 1;
	public final static int ADV = 2;
	public final static int VACATION = 3;
	public final static int VACATION_ALLOWANCE = 4;
	public final static int WUK = 5;
	public final static int KPT_GROSS = 6;
	public final static int VACATION_PAYED = 7;
	public final static int ADV_PAYED = 8;
	public final static int TVT_PAYED = 9;
	public final static int VACATION_BUDGET = 10;
	public final static int ADV_BUDGET = 11;
	public final static int TVT_BUDGET = 12;
	public final static int SICK = 13;
	public final static int BD = 14;
	
	public Payment(Context context, int year, int period) {
		data = new double[15];
		
		int contract = Preferences.getContract(context);
		
		data[WORKED] = ActMainV9.periodShifts.getWorkTime();
		double allowance = ActMainV9.periodShifts.getAllowance().getTotal();
		data[SICK] = ActMainV9.periodShifts.getSickHours();
		
		for (Holiday holiday : new HolidayDatabase(context).getHolidays(year, period)) {
			data[BD] += ShiftDatabase.db.averageInLast12Weeks(holiday.getYear(), holiday.getWeek(), holiday.getDay());
			DayShifts dayShifts =ActMainV9.periodShifts.getWeekShifts(holiday.getWeek()).getShifts()[holiday.getDay()];
			if (dayShifts.getWorkTime() > 0 && dayShifts.getAllowance().fixedDayAllowance == 0) {
				allowance += dayShifts.getWorkTime() - dayShifts.getAllowance().dayAllowance;
			}
		}
		data[ALLOWANCE] = Math.floor(allowance * 4) / 4;
		
		double total = data[WORKED] + data[ALLOWANCE] + data[SICK] + data[BD];
		
		if (contract <= 48) {
			double tvt = total - contract;
			if (tvt > 0 && contract != 0) {
				data[TVT_PAYED] = tvt;
				data[WORKED] -= tvt;
			}
			data[ADV] = total * ADV_PERC;
			data[VACATION] = total * VACATION_PERC;
			data[VACATION_ALLOWANCE] = total * VACATION_ALLOWANCE_PERC;
			data[WUK] = total * WUK_PERC;
			data[KPT_GROSS] = (total * KPT_GROSS_PERC) + data[SICK] + data[BD];
		} else {
			PeriodBudgetOld budget = Preferences.getBudget(context);
			
			// if no budget is available
			if (budget.getYear() > year || (budget.getYear() == year && budget.getPeriod() > period)) {
				data = null;
			} else {
				while (budget.getYear() < year || (budget.getYear() == year && budget.getPeriod() < period)) {
					budget.next(context, contract);
				}
				data[VACATION_PAYED] = budget.getWithdrawals()[Budget.TYPE_VACATION];
				data[ADV_PAYED] = budget.getWithdrawals()[Budget.TYPE_ADV];
				data[TVT_PAYED] = budget.getWithdrawals()[Budget.TYPE_TVT];
				data[VACATION_BUDGET] = budget.getWithdrawable(Budget.TYPE_VACATION, contract);
				data[ADV_BUDGET] = budget.getWithdrawable(Budget.TYPE_ADV, contract);
				data[TVT_BUDGET] = budget.getWithdrawable(Budget.TYPE_TVT, contract);
			}
		}
	}
	
	public boolean isValid() {
		return data != null;
	}
}
